/*
* @Author: An
* @Date:   2017-07-15 17:04:10
* @Last Modified by:   An
* @Last Modified time: 2017-07-16 13:25:37
*/

'use strict';
var _xj 				 = require('util/xj.js');
var _city 				 = require('util/city/index.js');
var _address 			 = require('service/address-service.js');
var templateAddressModal = require('./address-modal.string');

var addressModal = {
	show : function(option) {
		// option的绑定
		this.option 	 = option;
		this.option.data = option.data || {};
		this.$modalWrap  = $('.modal-wrap');
		// 渲染页面
		this.loadModal();
		// 绑定事件
		this.bindEvent();
	},
	loadModal : function() {
		var addressModalHtml = _xj.renderHtml(templateAddressModal, {
			isUpdate : this.option.isUpdate,
			data 	 : this.option.data
		});
		this.$modalWrap.html(addressModalHtml);
		// 加载省份
		this.loadProvince();
	},
	// 加载省份
	loadProvince : function() {
		var provinces 		= _city.getProvinces() || [],
			$provinceSelect = this.$modalWrap.find('#receiver-province');
		$provinceSelect.html(this.getSelectOption(provinces));
		// 如果是更新地址并且有省份信息，则回填省份
		if (this.option.isUpdate && this.option.data.receiverProvince) {
			$provinceSelect.val(this.option.data.receiverProvince);
			this.loadCity(this.option.data.receiverProvince);
		}
	},
	// 加载城市
	loadCity : function(provinceName) {
		var cities 		= _city.getCities(provinceName) || [],
			$citySelect = this.$modalWrap.find('#receiver-city');
		$citySelect.html(this.getSelectOption(cities));
		// 如果是更新地址并且有城市信息，则回填城市
		if (this.option.isUpdate && this.option.data.receiverCity) {
			$citySelect.val(this.option.data.receiverCity);
		}
	},
	// 获取表单收件人信息，并做校验
	getReceiverInfo : function() {
		var receiverInfo = {},
			result 		 = {
				status : false
			};
		receiverInfo.receiverName 	  = $.trim(this.$modalWrap.find('#receiver-name').val());
		receiverInfo.receiverProvince = this.$modalWrap.find('#receiver-province').val();
		receiverInfo.receiverCity 	  = this.$modalWrap.find('#receiver-city').val();
		receiverInfo.receiverPhone    = $.trim(this.$modalWrap.find('#receiver-phone').val());
		receiverInfo.receiverAddress  = $.trim(this.$modalWrap.find('#receiver-address').val());
		receiverInfo.receiverZip 	  = $.trim(this.$modalWrap.find('#receiver-zip').val());
		// 如果是修改操作，需要添加收货地址的ID
		if (this.option.isUpdate) {
			receiverInfo.id 	  = this.$modalWrap.find('#receiver-id').val();
		}
		// 表单校验
		if (!receiverInfo.receiverName) {
			result.errMsg = '请输入收件人姓名';
		} else if (!receiverInfo.receiverProvince || receiverInfo.receiverProvince === '请选择') {
			result.errMsg = '请选择收件人所在省份';
		} else if (!receiverInfo.receiverCity || receiverInfo.receiverCity === '请选择') {
			result.errMsg = '请选择收件人所在城市';
		} else if (!receiverInfo.receiverAddress) {
			result.errMsg = '请输入收件人详细地址';
		} else if (!receiverInfo.receiverPhone) {
			result.errMsg = '请输入收件人手机号';
		} else {
			// 所有校验通过
			result.status = true;
			result.data   = receiverInfo;
		}
		return result;
	},
	bindEvent : function() {
		var _this = this;
		this.$modalWrap.find('#receiver-province').change(function() {
			var selectedProvince = $(this).val();
			_this.loadCity(selectedProvince);
		});
		// 收货地址提交
		this.$modalWrap.find('.address-btn').click(function() {
			var receiverInfo = _this.getReceiverInfo(),
				isUpdate = _this.option.isUpdate;
			// 添加新地址并且校验通过
			if (!isUpdate && receiverInfo.status) {
				_address.save(receiverInfo.data, function(res) {
					_xj.successTips('地址添加成功');
					_this.hide();
					typeof _this.option.onSuccess === 'function' 
					&& _this.option.onSuccess(res);
				}, function(errMsg) {
					_xj.errorTips(errMsg);
				});
			} 
			// 更新地址并且校验通过
			else if (isUpdate && receiverInfo.status) {
				_address.update(receiverInfo.data, function(res) {
					_xj.successTips('地址修改成功');
					_this.hide();
					typeof _this.option.onSuccess === 'function' 
					&& _this.option.onSuccess(res);
				}, function(errMsg) {
					_xj.errorTips(errMsg);
				});
			} 
			// 校验不通过
			else {
				_xj.errorTips(receiverInfo.errMsg || '好像哪里不对了');
			}
		});
		// 保证点击modal内容区不关闭modal
		this.$modalWrap.find('.modal-con').click(function(e) {
			e.stopPropagation();
		});
		// 点击右上角关闭modal
		this.$modalWrap.find('.close').click(function() {
			_this.hide();
		});
		
	},
	// 获取下拉框的选项
	getSelectOption : function(optionArray) {
		var html = '<option>请选择</option>';
		for(var i = 0, length = optionArray.length; i < length; i++) {
			html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
		}
		return html;
	},
	// 关闭modal
	hide : function() {
		this.$modalWrap.empty();
	}
};
module.exports = addressModal;